﻿using hygge_imaotai.Domain;
using Newtonsoft.Json;
using System;
using System.Configuration;
using System.Linq;
using System.Windows;

namespace hygge_imaotai.UserInterface.UserControls {
    /// <summary>
    /// SystemConfigControl.xaml 的交互逻辑
    /// </summary>
    public partial class SystemConfigControl {
        public SystemConfigControl() {
            InitializeComponent();
        }

        private void BtnSave_OnClick(object sender, RoutedEventArgs e) {
            App.Config.AutoStart = cbxAutoStart.IsChecked.Value;
            App.Config.AutoBuy = cbxAutoBuy.IsChecked.Value;
            App.Config.BuyStartHour = tbxBuyStartHour.Text;
            App.Config.BuyStartMinute = tbxBuyStartMinuter.Text;
            App.Config.BuyStartSecond = tbxBuyStartSecond.Text;
            string str = tbxSplitProvince.Text.Trim();
            if (str.Length > 0)
                App.Config.ShopProvinceList = tbxSplitProvince.Text.Split(",");
            else
                App.Config.ShopProvinceList = Array.Empty<string>();
            str = tbxSplitCity.Text.Trim();
            if (str.Length > 0)
                App.Config.ShopCityList = tbxSplitCity.Text.Split(",");
            else
                App.Config.ShopCityList = Array.Empty<string>();

            new SystemRun().SetAutoStart(App.Config.AutoStart);

            App.WriteCache("config.json", JsonConvert.SerializeObject(App.Config));
            MessageBox.Show("参数保存成功，重启程序生效。", "提示", MessageBoxButton.OK);
        }

        private void SystemConfigControl_OnLoaded(object sender, RoutedEventArgs e) {
            cbxAutoStart.IsChecked = App.Config.AutoStart;
            cbxAutoBuy.IsChecked = App.Config.AutoBuy;
            tbxBuyStartHour.Text = App.Config.BuyStartHour;
            tbxBuyStartMinuter.Text = App.Config.BuyStartMinute;
            tbxBuyStartSecond.Text = App.Config.BuyStartSecond;
            tbxSplitProvince.Text = string.Join(",", App.Config.ShopProvinceList);
            tbxSplitCity.Text = string.Join(",", App.Config.ShopCityList);
        }

        private void BtnGitee_OnClick(object sender, RoutedEventArgs e) {
            Link.OpenInBrowser(ConfigurationManager.AppSettings["GiteeRelease"]);
        }

        private void BtnGiteeSource_OnClick(object sender, RoutedEventArgs e) {
            Link.OpenInBrowser(ConfigurationManager.AppSettings["Gitee"]);
        }

        private void BtnExit_OnClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();

        }
    }
}
